import {
  _decorator,
  Component,
  Label,
  macro,
  Node,
  RigidBody2D,
  Vec2,
} from "cc";
import { back } from "./back";
const { ccclass, property } = _decorator;

@ccclass("j")
export class j extends back {
  @property(RigidBody2D)
  cocos1: RigidBody2D;
  @property(Label)
  label: Label;

  moveDir = 1;
  tempVec: Vec2;
  start() {
    console.info(111,this.cocos1)
    this.schedule(
      () => {
        // 引力翻转
        this.cocos1.gravityScale = this.cocos1.gravityScale * -1;
      },
      3,
      macro.REPEAT_FOREVER,
      1.5
    );
  }

  update(deltaTime: number) {
    // 超出右边界，星星半径 12.5
    if (this.cocos1.node.position.x > 375 - 25) {
      this.moveDir = -1;
    }
    // 超出左边界
    if (this.cocos1.node.position.x < -(375 - 25)) {
      this.moveDir = 1;
    }
    // 进行水平线速度赋值
    this.tempVec = this.cocos1.linearVelocity;
    this.tempVec.x = this.moveDir * deltaTime * 200;
    this.cocos1.linearVelocity = this.tempVec;
    this.label.string = `线速度: ${this.cocos1.linearVelocity.toString()}`;
  }
}
